<!--title-->
<div class="mnav1">
	<div class="m_nav">
		<div class="nav_left">
			<em class="home_icon"><?=$this->sys_name?></em><span>&nbsp;&gt;&nbsp;</span><font><?=$this->title?></font>
		</div>
	</div>
</div>

<div class="content-table">
<div class="content-table-title"><?=$this->title?></div>
<br />
<?php if($this->msg):?>
<div class="replace"><?=$this->msg?></div>
<br />
<?php endif;?>
<div class="form-edit-task">

<?php
if($this->contract_id)
{
	$target_session = $_SESSION['contract-edit-type2'][$this->contract_id];	
}else{
	$target_session = $_SESSION['contract'];
}
?>
	
<form action="/contracts/add-sku-into-contract-submit" method="post" name="form1" onsubmit="submitform(this);return false;">
<table border="1" width="100%">
	<tr>
		<td align="right" width="100">相关经销商：</td>
		<td align="left">
			<select name="dealer_id" id="dealer_id">
				<option value="">无</option>
				<?php if(!empty($target_session['contract_info']['dealers_arr'])):?>
				<?php if(1 == count($target_session['contract_info']['dealers_arr'])){$selected = " selected='selected'";}else{$selected = "";}?>
				
				<?php foreach($target_session['contract_info']['dealers_arr'] as $dealer_id => $dealer_name):?>
				<option value="<?=$dealer_id?>" <?=$selected?>>【<?=$dealer_id?>】<?=$dealer_name?></option>
				<?php endforeach;?>
				
				<?php endif;?>
			</select>
		</td>
	</tr>
	<tr>
		<td align="right">产品分类：</td>
		<td align="left">
			<select name="product_id" id="product_id" onchange="GetSku(this.options[this.options.selectedIndex].value, 1, 0);">
				<option value="">【选择】</option>
				<?php foreach($this->products as $product_id => $product_name):?>
				<?php if((!empty($this->existed_product_ids) && in_array($product_id, $this->existed_product_ids)) || empty($this->existed_product_ids)):?>
				<option value="<?=$product_id?>"><?=$product_name?></option>
				<?php endif;?>
				<?php endforeach;?>
			</select>
		</td>
	</tr>
	<tr>
		<td align="right">刊例：</td>
		<td align="left">
			<em id="sku_space_board"><em id="sku_space_0"></em></em><input type="hidden" name="sku_space_count" id="sku_space_count" value="0" />
			<input type="hidden" name="sku_id" id="sku_id" />
		</td>
	</tr>
	<tr>
		<td align="right">刊例单价：</td>
		<td align="left">&nbsp;<em id="unit_price_space"></em></td>
	</tr>
	<tr>
		<td align="right">数量：</td>
		<td align="left">&nbsp;<em id="quantity_space"></em></td>
	</tr>
	<tr>
		<td align="right">刊例价：</td>
		<td align="left">&nbsp;<em id="rrp_space"></em></td>
	</tr>
	<tr>
		<td align="right">标准售价：</td>
		<td align="left">&nbsp;<em id="claim_amount_space"></em></td>
	</tr>
	<tr>
		<td align="right">实际售价：</td>
		<td align="left">&nbsp;<em id="actual_pay_space"></em></td>
	</tr>
	<tr>
		<td align="right">折扣：</td>
		<td align="left">&nbsp;<em id="discount"></em></td>
	</tr>
	<tr>
		<td align="right">开始日期：</td>
		<td align="left">&nbsp;<input type="text" name="start_date" id="start_date" style="width: 100px" autocomplete="off"></td>
	</tr>
	<tr>
		<td align="right">结束日期：</td>
		<td align="left">&nbsp;<input type="text" name="expired_date" id="expired_date" style="width: 100px" autocomplete="off"></td>
	</tr>
    <tr id="hxb">
        <td align="right">汇销宝账号：</td>
        <td align="left"><input type="text" name="hxb_account" id="hxb_account" placeholder="汇销宝登录手机号" style="width: 200px">（必填）</td>
    </tr>
	<tr id="platform">
		<td align="right">托管平台：</td>
		<td align="left">
			<?php if(!empty($this->services)):?>
			<?php foreach($this->services as $service_id => $service_name):?>
			<input type="checkbox" name="service_ids[]" id="service_id_<?=$service_id?>" value="<?=$service_id?>" /><label for="service_id_<?=$service_id?>"> <?=$service_name?></label>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="start_date_<?=$service_id?>" id="start_date_<?=$service_id?>" placeholder="开始日期" autocomplete="off" style="width: 100px"> 至 <input type="text" name="expired_date_<?=$service_id?>" id="expired_date_<?=$service_id?>" placeholder="结束日期" autocomplete="off" style="width: 100px"><br />
			<?php endforeach;?>
			<?php endif;?>
		</td>
	</tr>
</table>
<br /><br />
<input type="button" class="buttonlong" value="提交" onclick="submitform();return false;" />
<br /><br />
<br /><br />
<?php if($this->contract_id):?>
<input type="hidden" name="contract_id" value="<?=$this->contract_id?>" />
<?php endif;?>
</form>
</div>
<br/><br/>
</div>

<script>
$("#platform").hide();
$("#hxb").hide();

function num(th){
	th.value = th.value.replace(/[^\d\.]*$/g, '');
	
	var actual_pay = th.value;
	var rrp = parseFloat(document.getElementById("rrp_space").innerHTML);
	
	if(rrp == 0)
	{
		document.getElementById("discount").innerHTML = 0;
	}else{
		document.getElementById("discount").innerHTML = parseFloat(actual_pay / rrp * 100).toFixed(0) + "%";
	}
	
	var sku_id = document.getElementById("sku_id").value;
	
	if(sku_id == 961)
	{
		var actual_pay = document.getElementById("new_actual_pay").value;
		
		$.ajax({
			type: "post",
			url : "/ajax/get-961-discount",
			dataType:'json',
            async:false,
			data: {
					'value': actual_pay
				  },
				success: function(json){
					if(json.result == 1) {
						document.getElementById("new_bonus").value = parseFloat(json.value).toFixed(2);
                    }
			}
		});
	}
}

$(function() {
	$( "#start_date" ).datepicker({ dateFormat: 'yy-mm-dd'});
});
$(function() {
	$( "#expired_date" ).datepicker({ dateFormat: 'yy-mm-dd'});
});

<?php if(!empty($this->services)):?>
<?php foreach($this->services as $service_id => $service_name):?>
$(function() {
	$( "#start_date_<?=$service_id?>" ).datepicker({ dateFormat: 'yy-mm-dd', 
    onSelect: function(){
      ReCalStartDate(<?=$service_id?>);
    }
  });
});
$(function() {
	$( "#expired_date_<?=$service_id?>" ).datepicker({ dateFormat: 'yy-mm-dd', 
    onSelect: function(){
      ReCalExpiredDate();
    }
  });
});
<?php endforeach;?>
<?php endif;?>

function ReCalStartDate(service_id)
{
	var start_date = [];
	
	<?php if(!empty($this->services)):?>
	<?php foreach($this->services as $service_id => $service_name):?>
	if( document.form1.start_date_<?=$service_id?>.value.length > 0 )
	{
		start_date.push(document.form1.start_date_<?=$service_id?>.value);
	}
	<?php endforeach;?>
	<?php endif;?>
	
	var min = start_date[0];

	for(var i=1;i<start_date.length;i++){ 

	  if(min>start_date[i])min=start_date[i];

	}

	document.form1.start_date.value = min;
	
	var el = document.getElementById('service_id_' + service_id);
	el.checked = true;
}

function ReCalExpiredDate()
{
	var expired_date = [];
	
	<?php if(!empty($this->services)):?>
	<?php foreach($this->services as $service_id => $service_name):?>
	if( document.form1.expired_date_<?=$service_id?>.value.length > 0 )
	{
		expired_date.push(document.form1.expired_date_<?=$service_id?>.value);
	}
	<?php endforeach;?>
	<?php endif;?>
	
	var max = expired_date[0];

	for(var i=1;i<expired_date.length;i++){ 

	  if(max<expired_date[i])max=expired_date[i];

	}

	document.form1.expired_date.value = max;
}

function submitform() 
{
	
	if( document.form1.sku_id.value.length == 0 )
	{
		alert("产品不能为空。");
		return false;
	}else if( document.form1.new_sku_quantity.value.length == 0 )
	{
		alert("数量不能为空。");
		return false;
	}else if( document.form1.start_date.value.length == 0 )
	{
		alert("开始日期不能为空。");
		return false;
	}else if( document.form1.expired_date.value.length == 0 )
	{
		alert("结束日期不能为空。");
		return false;
	}else if( document.form1.new_actual_pay.value.length == 0 )
	{
		alert("实际售价不能为空。");
		return false;
	}else if($("#product_id").val() == 2 && $("#hxb_account").val() == ""){
        alert("汇销宝账号不能为空。");
        return false;
    }
	
	form1.submit();
}

function GetSku(id, type, self_count_id)
{
	var sku_space_count = parseInt(document.getElementById("sku_space_count").value);

    if($("#product_id").val() == "2"){
        $("#hxb").show();
    }else{
        $("#hxb").hide();
    }

    if(type == 1)
	{
		var start = 0;
		for(i=start; i < sku_space_count; i++){
			var sku_select_name = "sku_select_" + i;
			document.getElementById(sku_select_name).outerHTML = "";
		}
		
		var sku_space_name = "sku_space_" + sku_space_count;
		
		document.getElementById(sku_space_name).outerHTML = "<em id='sku_space_"+start+"'></em>";
		document.getElementById("sku_space_count").value = start;
	}
	else if(self_count_id + 1 < sku_space_count)
	{
		var start = self_count_id + 1;
		for(i=start; i < sku_space_count; i++){
			var sku_select_name = "sku_select_" + i;
			document.getElementById(sku_select_name).outerHTML = "";
		}
		
		var sku_space_name = "sku_space_" + sku_space_count;
		
		document.getElementById(sku_space_name).outerHTML = "<em id='sku_space_"+start+"'></em>";
		document.getElementById("sku_space_count").value = start;
	}else{
		var start = sku_space_count;
	}

	$.ajax({
			type: "post",
			url : "/ajax/get-sku",
			dataType:'json',
            async:false,
			data: {
					'id': id,
					'type': type,
					'sku_space_count': start,
					'in_edit_mode': <?php if($_SESSION['contract-edit-type2'][$this->contract_id]){echo 1;}else{echo 0;}?>
				  },
				success: function(json){
					if(json.result == 1 && typeof(json.type) != "undefined") {
						if(json.type == 1) //展示分类
						{
							var newcount = parseInt(json.sku_space_count) + 1;
							var arr = json.options;
							var txt = "<select onchange='GetSku(this.options[this.options.selectedIndex].value, 2, "+parseInt(json.sku_space_count)+");' id='sku_select_"+parseInt(json.sku_space_count)+"'><option value=''>【请选择】</option>";

							for(i=0; i < arr.length; i++){
								txt += "<option value='" + arr[i]['sku_id'] + "'>" + arr[i]['sku_name'] + "</option>";
							}
							
							txt += "</select><em id='sku_space_" + newcount + "'></em>";
							
							var sku_space_name = "sku_space_" + json.sku_space_count;
							
							document.getElementById(sku_space_name).outerHTML = txt;
							document.getElementById("sku_space_count").value = newcount;
						}else{ //展示产品信息
							var arr = json.sku_info;
							
							if(arr.sku_id == 961)
							{
								document.getElementById("unit_price_space").innerHTML = arr.unit_price;
								document.getElementById("quantity_space").innerHTML = "<input type='text' name='new_sku_quantity' id='new_sku_quantity' value='1' style='width: 50px' onkeyup='Cal(this)' />";
								document.getElementById("actual_pay_space").innerHTML = "<input type='text' name='new_actual_pay' id='new_actual_pay' style='width: 80px' value='' onkeyup='num(this)' />&nbsp;&nbsp;&nbsp;&nbsp;返点金额 <input type='text' name='new_bonus' id='new_bonus' style='width: 80px' value='' placeholder='返点金额' />";
								document.getElementById("rrp_space").innerHTML = arr.unit_price;
								document.getElementById("claim_amount_space").innerHTML = arr.unit_price;
								document.getElementById("sku_id").value = arr.sku_id;
								document.getElementById("discount").innerHTML = "100%";
							}else{
								var claim_amount = parseFloat(arr.unit_price) / 2;
								document.getElementById("unit_price_space").innerHTML = arr.unit_price;
								document.getElementById("quantity_space").innerHTML = "<input type='text' name='new_sku_quantity' id='new_sku_quantity' value='1' style='width: 50px' onkeyup='Cal(this)' />";
								document.getElementById("actual_pay_space").innerHTML = "<input type='text' name='new_actual_pay' id='new_actual_pay' style='width: 80px' value='' onkeyup='num(this)' />";
								document.getElementById("rrp_space").innerHTML = arr.unit_price;
								document.getElementById("claim_amount_space").innerHTML = claim_amount;
								document.getElementById("sku_id").value = arr.sku_id;
								document.getElementById("discount").innerHTML = "50%";
							}
						}
                    }else{ //没找到，初始化表单
						document.getElementById("sku_space_board").innerHTML = "<em id='sku_space_0'></em>";
						document.getElementById("unit_price_space").innerHTML = "";
						document.getElementById("quantity_space").innerHTML = "";
						document.getElementById("actual_pay_space").innerHTML = "";
						document.getElementById("rrp_space").innerHTML = "";
						document.getElementById("claim_amount_space").innerHTML = "";
						document.getElementById("sku_space_count").value = 0;
						document.getElementById("discount").innerHTML = "";
					}
					
					if(json.is_product == 1)
					{
						if(json.display_platform == 1)
						{
							$("#platform").show();
							$('#start_date').hide();
							$('#expired_date').hide();
						}else{
							$("#platform").hide();
							$('#start_date').show();
							$('#expired_date').show();
						}
					}
			}
		});
}

function Cal(th){
	th.value = th.value.replace(/[^\d\.]*$/g, '');
	var qty = th.value;
	var unit_price = parseFloat(document.getElementById("unit_price_space").innerHTML);
	var rrp = (qty * unit_price);
	
	var sku_id = document.getElementById("sku_id").value;
	
	if(sku_id == 961)
	{
		var claim_amount = rrp;
		
		$.ajax({
			type: "post",
			url : "/ajax/get-961-discount",
			dataType:'json',
            async:false,
			data: {
					'value': claim_amount
				  },
				success: function(json){
					if(json.result == 1) {
						document.getElementById("new_bonus").value = parseFloat(json.value).toFixed(2);
                    }
			}
		});
	}else{
		var claim_amount = rrp / 2;
	}
	
	document.getElementById("rrp_space").innerHTML = parseFloat(rrp).toFixed(2);
	document.getElementById("claim_amount_space").innerHTML = parseFloat(claim_amount).toFixed(2);
	document.getElementById("new_actual_pay").value = parseFloat(claim_amount).toFixed(2);
}

function AddSku()
{
	var sku_id = document.getElementById("sku_id").value;
	var new_sku_quantity = document.getElementById("new_sku_quantity").value;
	var new_actual_pay = document.getElementById("new_actual_pay").value;
	
	$.ajax({
			type: "post",
			url : "/ajax/add-sku-into-contract",
			dataType:'json',
            async:false,
			data: {
					'sku_id': sku_id,
					'new_sku_quantity': new_sku_quantity,
					'new_actual_pay': new_actual_pay
				  },
				success: function(json){
					if(json.result == 1) {
						window.location.href = "/contracts/add-contract-step2/msg/1";
                    }else{
						window.location.href = "/contracts/add-contract-step2/msg/2";
					}
			}
		});
}
</script>
